package com.wallet.provider.service.impl;

import com.github.pagehelper.Page;

import com.wallet.common.vo.PageResult;
import com.wallet.provider.mapper.RoleMapper;
import com.wallet.provider.mapper.SysUserMapper;
import com.wallet.provider.mapper.UserRoleMapper;
import com.wallet.provider.model.SysUser;
import com.wallet.provider.model.UserRole;
import com.wallet.provider.model.vo.UserRoleVo;
import com.wallet.provider.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<UserRoleVo> getUserRoleList(UserRoleVo userRoleVo, int pageNum, int pageSize, String orderName, String orderType) {
        Page<UserRoleVo>userRoleVos=userRoleMapper.selectByPageNumSize(userRoleVo,pageNum, pageSize, orderName, orderType);
        return PageResult.getPageResult(userRoleVos);
    }

    @Override
    public Boolean addUserRole(UserRoleVo userRoleVo) {
        SysUser sysUser=new SysUser();
        sysUser.setUsername(userRoleVo.getUsername());
        sysUser.setPassword(userRoleVo.getPassword());
        sysUser.setEmail(userRoleVo.getEmail());
        sysUser.setIp(userRoleVo.getIp());
        sysUser.setLastLoginTime(userRoleVo.getLastLoginTime());
        sysUser.setGender((byte)0);
        sysUserMapper.insert(sysUser);
        UserRole userRole=new UserRole();
        userRole.setRoleId(userRoleVo.getRoleId());
        userRole.setUserId(sysUser.getId());
        return userRoleMapper.insert(userRole) > 0;
    }

    @Override
    public UserRoleVo getUserRoleById(Long id) {
        return userRoleMapper.selectUserRoleById(id);
    }

    @Override
    public Boolean updateUserRole(UserRoleVo userRoleVo) {
        SysUser sysUser=sysUserMapper.selectByPrimaryKey(userRoleVo.getUserId());
        sysUser.setId(userRoleVo.getUserId());
        sysUser.setUsername(userRoleVo.getUsername());
        sysUser.setPassword(userRoleVo.getPassword());
        sysUser.setEmail(userRoleVo.getEmail());
        int res=sysUserMapper.updateByPrimaryKey(sysUser);

        UserRole userRole=new UserRole();
        userRole.setUserId(userRoleVo.getUserId());
        userRole.setRoleId(userRoleVo.getRoleId());
        res+=userRoleMapper.updateUserRole(userRole);


        return res> 1;
    }

    @Override
    public Boolean deleteUserRole(UserRole userRole) {
        return userRoleMapper.deleteByPrimaryKey(userRole)>0;
    }
}
